import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/log/login')
    },
    {
      path: '/backHome',
      name: 'backHome',
      redirect:'/welcome',
      component: () => import('@/back-home'),
      children:[
        {
          path:'/welcome',
          name:'welcome',
          component:()=>import('@/component/welcome')
        },
        {
          path:'/users',
          name:'users',
          component:()=>import('@/component/用户管理/users')
        },
        {
          path:'/rights',
          name:'rights',
          component:()=>import('@/component/权限管理/rights')
        },
        {
          path:'/roles',
          name:'roles',
          component:()=>import('@/component/权限管理/roles')
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = window.sessionStorage.getItem('token')
  if (to.path == '/login') {
    if (token) {
      next('/backHome')
    }
  } else {
    if (!token) {
      next('/login')
    }
  }
  // }

  next();
})


export default router